package com.cloudcc.mobile.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.util.VerificateUtils;
import com.cloudcc.mobile.view.activity.NewContactDetails;
import com.mypage.model.ContactData;

import java.util.List;

/**
 * Created by zys on 2017/5/9.
 */

public class NewContactAdapter extends BaseAdapter {
        private LayoutInflater mInflater;
        private Context context;
        private  List<ContactData> mData;
        private  String recordId,layoutId,relatedlistId;
        public NewContactAdapter(Context context, List<ContactData> list,String recordId,String layoutId,String relatedlistId){
            this.mInflater = LayoutInflater.from(context);
            this.context = context;
            this.mData = list;
            this.recordId = recordId;
            this.layoutId = layoutId;
            this.relatedlistId = relatedlistId;
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mData.size()>0?mData.size():0;
        }
    public void changeData() {
        notifyDataSetChanged();
    }
        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return mData.get(arg0);
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }
//    TextView TextView;
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            ViewHolder holder = null;
            if (convertView == null) {
                holder=new ViewHolder();
                convertView = mInflater.inflate(R.layout.contact_item, null);
                holder.contactname = (TextView) convertView.findViewById(R.id.contactitem_name);
                holder.contactdianhua= (TextView) convertView.findViewById(R.id.contactitem_dianhua);
                holder.contactjuese= (TextView) convertView.findViewById(R.id.tv2);
//                TextView= (TextView) convertView.findViewById(R.id.newcontact_itemjuese11);
                holder.contactzhiwu= (TextView) convertView.findViewById(R.id.contactitem_zhiwu);
                holder.imageView = (ImageView) convertView.findViewById(R.id.contact_item_tag);
                holder.linearLayout = (LinearLayout) convertView.findViewById(R.id.contactitem);
                convertView.setTag(holder);
            }else {
                holder = (ViewHolder)convertView.getTag();
            }
            if("1".equals(mData.get(position).isdefault)){
                holder.imageView.setVisibility(View.VISIBLE);
            }else{
                holder.imageView.setVisibility(View.GONE);
            }
            try {
                holder.contactname.setText(mData.get(position).name);
            }catch (Exception ex){

            }
            try {
                holder.contactdianhua.setText(mData.get(position).dianhua);
//                holder.contactdianhua.setText("13583332657");
            }catch (Exception ex){

            }
            try {
                holder.contactjuese.setText(mData.get(position).juese);
//                TextView.setText(mData.get(position).juese);
            }catch (Exception ex){


            }
            try {
                holder.contactzhiwu.setText(mData.get(position).zhuwu);
            }catch (Exception ex){

            }
            holder.linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context,NewContactDetails.class);
                    intent.putExtra("id",mData.get(position).id);
                    intent.putExtra("name",mData.get(position).name);
                    intent.putExtra("jueseid",mData.get(position).jueseId);
                    intent.putExtra("creatname",mData.get(position).creatName);
                    intent.putExtra("creattime",mData.get(position).creatTime);
                    intent.putExtra("creatid",mData.get(position).creatId);
                    intent.putExtra("kehuname",mData.get(position).kehu_name);
                    intent.putExtra("kehuid",mData.get(position).kehu_id);
                    intent.putExtra("juese",mData.get(position).juese);
                    intent.putExtra("lianxirenname",mData.get(position).name);
                    intent.putExtra("lianxirenjueseid",mData.get(position).id);
                    intent.putExtra("lianxirenid",mData.get(position).lianxiId);
                    intent.putExtra("isdefault",mData.get(position).isdefault);
                    intent.putExtra("recordId",recordId);
                    intent.putExtra("layoutId",layoutId);
                    intent.putExtra("relatedlistId",relatedlistId);
                    intent.putExtra("accountpermiss",mData.get(position).accountpermiss);

                    context.startActivity(intent);
//                    Toast.makeText(context,"点击全部",1).show();
                }
            });
            holder.contactdianhua.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(VerificateUtils.isMobile(mData.get(position).dianhua)) {
                        Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+mData.get(position).dianhua));
                        context.startActivity(intent);
//                        Toast.makeText(context,"点击电话",1).show();
                    }else{
                        Toast.makeText(context, R.string.dianhuageshi,Toast.LENGTH_SHORT).show();
                    }
//                    Toast.makeText(context,"点击电话",1).show();
                }
            });


//            holder.imageView.setVisibility(View.GONE);
            return convertView;
        }
        class ViewHolder{
            public TextView contactname,contactzhiwu,contactdianhua,contactjuese;
            public ImageView imageView;
            public LinearLayout linearLayout;
        }
    }
